<?php

/**
 * File: libs/english_trainee.php
 *
 * Handles the functioniallity of the english trainee
 *
 * @author     Philipp Petzold <pp@dezign.de>
 * @version    1.0
 */

// table name defines
define('MYSQL_TABLE_GAME_LEVELS', 'et_game_levels');

class EnglishTrainee {

	// singleton instance object
	private static $instance;

    /**
     *  do nothing
     * 
     *  @return  void
     */
    private function __construct()
    {
        // ...
    }

    /**
     *  create all needed tables in database
     *
     *  @global  PDO     $database          global database object
     * 
     *  @return  boolean                    dberror status
     */
    public function createDatabaseStructure()
    {
        global $database;

        // create game level table, if not exists
        $query = "CREATE TABLE IF NOT EXISTS " . MYSQL_TABLE_GAME_LEVELS . " (
                    `id` INT(255) NOT NULL auto_increment,
                    `level` INT(255) NOT NULL,
                    `word` VARCHAR(255) COLLATE 'utf8_general_ci' NOT NULL,
                    `image` VARCHAR(255) COLLATE 'utf8_general_ci' NOT NULL,
                    `created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    `updated` TIMESTAMP,
                    PRIMARY KEY  (`id`)
                    ) ENGINE=InnoDB CHARSET=latin1";

        $result = $database->query($query);

        if (!$result)
        {
            print_r($database->errorInfo());
            return FALSE;
        }
            
        return TRUE;
    }

    /**
     *  creates a new level by the given level, word and image
     *
     *  @global  PDO     $database          global database object
     *  @param   int     $level             the specified level
     *  @param   string  $word              word, which the user have to guess
     *  @param   string  $image             path to uploaded image
     * 
     *  @return  boolean                    dberror status
     */
    public function insertGameLevel($level, $word, $image)
    {
        global $database;

        $query = $database->prepare("INSERT INTO `".MYSQL_TABLE_GAME_LEVELS."` (`level`, `word`, `image`, `updated`) VALUES (:level, :word, :image, NOW())");

        $query->bindParam('level', $level, PDO::PARAM_INT);
        $query->bindParam('word',  $word);
        $query->bindParam('image', $image);

        return $query->execute();
    }

    /**
     *  update an existing level by the given level, word and image
     *
     *  @global  PDO     $database          global database object
     *  @param   int     $id                id of the updating level
     *  @param   int     $level             the specified level
     *  @param   string  $word              word, which the user have to guess
     *  @param   string  $image             path to uploaded image
     * 
     *  @return  boolean                    dberror status
     */
    public function updateGameLevel($id, $level, $word, $image)
    {
        global $database;

        $query = $database->prepare("UPDATE `".MYSQL_TABLE_GAME_LEVELS."` SET `level`=:level, `word`=:word, `image`=:image, `updated`=NOW() WHERE `id`= :id");

        $query->bindParam('id',    $id, PDO::PARAM_INT);
        $query->bindParam('level', $level, PDO::PARAM_INT);
        $query->bindParam('word',  $word);
        $query->bindParam('image', $image);

        return $query->execute();
    }

    /**
     *  returns a random word by the given level (only random, if there is more than one existing word for the given level)
     *
     *  @global  PDO     $database          global database object
     *  @param   int     $level             the specified level
     * 
     *  @return  boolean                    dberror status
     */
    public function retrieveGameLevel($level)
    {
        global $database;

        $query = $database->prepare("SELECT * FROM ".MYSQL_TABLE_GAME_LEVELS." WHERE level = :level");
        $query->bindParam('level', $level, PDO::PARAM_INT);

        $temp = array();

        if($query->execute())
        {
            while ($row = $query->fetch(PDO::FETCH_ASSOC))
            {
                $temp[] = $row;
            }
        }
        else
        {
            print_r( $database->errorInfo() );
            return FALSE;
        }
        
        return $temp;
    }

    /**
     *  static instance return for singleton
     * 
     *  @return  EnglishTrainee             singleton instance
     */
    public static function getInstance()
    {
        if (!self::$instance)
        {
            self::$instance = new EnglishTrainee();
        }
        return self::$instance;
    } 
}

?>